package com.aqie.arithmetic.datastructure;

public class QuickUnion extends UFBase{
    public QuickUnion(int N) {
        super(N);
    }

    protected int find(int p){
        while (p != id[p]) p = id[p];
        return p;
    }

    private void union(int p, int q){
        // 将 pq 的根节点统一
        int pRoot = find(p);
        int qRoot = find(q);
        if (pRoot == qRoot) return;
        id[pRoot] = qRoot;
        count--;
    }
}
